//
//  CourseInfoViewController.m
//  Grades
//
//  Created by Andre Kradolfer on 4/2/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CourseInfoViewController.h"


@implementation CourseInfoViewController

- (id)initWithStyle:(UITableViewStyle)style
{
	if (self = [super initWithStyle:style])
	{
		[self setTitle:self.navigationItem.title];
	}
	return self;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	assignments = [[NSMutableArray alloc]init];
	
	NSMutableArray *examArray = [NSMutableArray arrayWithObjects:@"Exam 1", @"Exam 2", @"Exam 3", nil];
	NSDictionary *examDict = [NSDictionary dictionaryWithObject:examArray forKey:@"Assignments"];
	
	NSMutableArray *labArray = [NSMutableArray arrayWithObjects:@"Lab 1", @"Lab 2",@"Lab 3",@"Lab 4", nil];
	NSDictionary *labDict = [NSDictionary dictionaryWithObject:labArray forKey:@"Assignments"];
	
	NSMutableArray *essayArray = [NSMutableArray arrayWithObjects:@"Essay 1", @"Essay 2", nil];
	NSDictionary *essayDict = [NSDictionary dictionaryWithObject:essayArray forKey:@"Assignments"];
	
	NSMutableArray *quizArray = [NSMutableArray arrayWithObjects:@"Quiz 1", @"Quiz 2",@"Quiz 3",@"Quiz 4",@"Quiz 5", nil];
	NSDictionary *quizDict = [NSDictionary dictionaryWithObject:quizArray forKey:@"Assignments"];
	
	
	[assignments addObject:examDict];
	[assignments addObject:labDict];
	[assignments addObject:essayDict];
	[assignments addObject:quizDict];
}
// Prepare UITableView to display data
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [assignments count];
}

// Customize the number of rows in the table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
{
	// Number of rows it should expect, based on the section
	NSDictionary *dictionary = [assignments objectAtIndex:section];
	NSMutableArray *array = [dictionary objectForKey:@"Assignments"];
	return [array count];
	
}

// Display the header text
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	switch (section) {
		case 0:
			return @"Exams";
			break;
		case 1:
			return @"Labs";
			break;
		case 2:
			return @"Essays";
			break;
		case 3:
			return @"Quizes";
			break;
		default:
			break;
	}
	return @"null";
}

// Display data in a table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
				autorelease];
	}
	
	
	NSDictionary *dictionary = [assignments objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Assignments"];
	NSString *cellValue = [array objectAtIndex:indexPath.row];
	cell.text = cellValue;
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	// get the selected class
	/*NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Courses"];
	NSString *selectedClass = [array objectAtIndex:indexPath.row];
	
	GradesViewController *gradesView = [[GradesViewController alloc] init];
	gradesView.navigationItem.title = selectedClass;
	[self.navigationController pushViewController:gradesView animated:YES];
	[gradesView release];
	gradesView = nil;*/
	NSLog(@"%d", indexPath);
}

/*- (void)tableView:(UITableView *)tableView setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    // Calculate the index paths for all of the placeholder rows based on the number of items in each section.
    NSArray *indexPaths = [NSArray arrayWithObjects:
						   [NSIndexPath indexPathForRow:[[[assignments objectAtIndex:0] valueForKeyPath:@"Assignment.@count"] intValue] inSection:0],
						   [NSIndexPath indexPathForRow:[[[assignments objectAtIndex:1] valueForKeyPath:@"Assignment.@count"] intValue] inSection:1],
						   [NSIndexPath indexPathForRow:[[[assignments objectAtIndex:2] valueForKeyPath:@"Assignment.@count"] intValue] inSection:2],
						   [NSIndexPath indexPathForRow:[[[assignments objectAtIndex:3] valueForKeyPath:@"Assignment.@count"] intValue] inSection:3], nil];
    [tableView beginUpdates];
    [tableView setEditing:editing animated:YES];
    if (editing) {
        // Show the placeholder rows
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    } else {
        // Hide the placeholder rows.
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    }
    [tableView endUpdates];
}*/

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
	NSDictionary *dictionary = [assignments objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Assignments"];

    if(indexPath.row == [array count] - 1) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
	

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSDictionary *dictionary = [assignments objectAtIndex:indexPath.section];
	NSMutableArray *array = [dictionary objectForKey:@"Assignments"];
	if(indexPath.row < [array count] -1){
		return YES;
	}else {
		return	NO;
	}

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView beginUpdates];
	if (editingStyle == UITableViewCellEditingStyleDelete)
	{
		NSDictionary *dictionary = [assignments objectAtIndex:indexPath.section];
		NSMutableArray *array = [dictionary objectForKey:@"Assignments"];
		[array removeObjectAtIndex:indexPath.row];
		[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }else if(editingStyle == UITableViewCellEditingStyleInsert) {
		NSDictionary *dictionary = [assignments objectAtIndex:indexPath.section];
		NSMutableArray *array = [dictionary objectForKey:@"Assignments"];
        [array addObject:@"New Row Added"];
        [tableView reloadData];        
    }
	
	[tableView endUpdates];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}


@end
